import logging
from pathlib import Path
from datetime import datetime

def setup_logger(output_dir: Path) -> logging.Logger:
    """
    Setup and configure the logger
    """
    # 禁用根日志记录器
    root_logger = logging.getLogger()
    root_logger.handlers = []
    root_logger.propagate = False
    root_logger.disabled = True
    
    # 确保输出目录是绝对路径
    output_dir = Path(output_dir).resolve()
    
    # Create logs directory if it doesn't exist
    log_dir = output_dir / 'logs'
    log_dir.mkdir(exist_ok=True)
    
    # Create logger
    logger = logging.getLogger('homework_grading')
    logger.setLevel(logging.INFO)
    logger.handlers = []  # 清空所有handler
    logger.propagate = False  # 不向上冒泡
    
    # Create handlers
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    log_file = log_dir / f'grading_{timestamp}.log'
    
    file_handler = logging.FileHandler(str(log_file), encoding='utf-8')
    console_handler = logging.StreamHandler()
    
    # Create formatters and add it to handlers
    log_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(log_format)
    console_handler.setFormatter(log_format)
    
    # Add handlers to the logger
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)
    
    return logger 